#include"Stack.h"
#include"Queue.h"




void test1()
{
	Stack st;
	StackInit(&st);
	StackPush(&st, 1);
	StackPush(&st, 2);
	StackPush(&st, 3);
	StackPush(&st, 4);
	while (!StackEmpty(&st))
	{
		printf("%d ", StackTop(&st));
		StackPop(&st);
	}

}

void test2()
{
	Queue q;
	QueueInit(&q);
	QueuePush(&q, 1);
	QueuePush(&q, 2);
	QueuePush(&q, 3);
	QueuePush(&q, 4);
	printf("%d\n", QueueSize(&q));
	QueuePop(&q);
	printf("%d\n", QueueSize(&q));
	QueuePop(&q);
	printf("%d\n", QueueSize(&q));
	QueuePop(&q);
	printf("%d\n", QueueSize(&q));
	QueuePop(&q);
	printf("%d\n", QueueSize(&q));
}






typedef struct {
	int* a;
	int front;
	int rear;
	int k;
} MyCircularQueue;


MyCircularQueue* myCircularQueueCreate(int k) {
	MyCircularQueue* mq = (MyCircularQueue*)malloc(sizeof(MyCircularQueue));
	int* ptr = (int*)malloc(sizeof(int) * (k + 1));
	mq->a = ptr;
	mq->front = mq->rear = 0;
	mq->k = k;
	return mq;
}

bool myCircularQueueEnQueue(MyCircularQueue* obj, int value) {
	if (myCircularQueueIsFull(obj))
		return false;
	if (obj->rear == obj->k)
	{
		obj->a[obj->rear] = value;
		obj->rear = 0;
		return true;
	}
	obj->a[obj->rear] = value;
	obj->rear += 1;
	return true;
}

bool myCircularQueueDeQueue(MyCircularQueue* obj) {
	if (myCircularQueueIsEmpty(obj))
		return false;
	if (obj->front == obj->k)
	{
		obj->front = 0;
		return true;
	}
	obj->front++;
	return true;
}

int myCircularQueueFront(MyCircularQueue* obj) {
	if (myCircularQueueIsEmpty(obj))
		return -1;
	return obj->a[obj->rear];

}

int myCircularQueueRear(MyCircularQueue* obj) {
	if (myCircularQueueIsEmpty(obj))
		return -1;

	if (obj->front == 0)
		return obj->a[obj->front + obj->k];
	return obj->a[obj->front - 1];

}

bool myCircularQueueIsEmpty(MyCircularQueue* obj) {
	return (obj->rear + 1) % (obj->k + 1) == obj->front;
}

bool myCircularQueueIsFull(MyCircularQueue* obj) {
	return obj->front == obj->rear;
}

void myCircularQueueFree(MyCircularQueue* obj) {
	free(obj->a);
	free(obj);
}




int main()
{
	//test1();
	test2();
	return 0;
}

